package com.tiger.web.controller.project;

import com.tiger.common.annotation.Log;
import com.tiger.common.constant.UserConstants;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.framework.web.service.TokenService;
import com.tiger.system.domain.SysConfig;
import com.tiger.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 【预警期限】Controller
 *
 * @author standard
 * @date 2022-09-23
 */
@Api(tags = "预警期限配置")
@RestController
@RequestMapping("/system/warning")
public class WarningController extends BaseController {
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询参数值
     */
    @ApiOperation(value = "查询合同预警/账单预警")
    @GetMapping( "/configKey/getWarningConfigTask")
    public AjaxResult getWarningConfigTask() {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(configService.getWarningConfigTask(loginUser.getUser().getDeptId()));
    }


    @ApiOperation(value = "批量修改参数配置  账单预警")
//    @PreAuthorize("@ss.hasPermi('system:config:edit')")
//    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping( "edit/configs")
    public AjaxResult edit(@Validated @RequestBody List<SysConfig> configs) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        int i = 0;
        for (SysConfig config : configs) {
            config.setCompanyId(loginUser.getUser().getDeptId());
            if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
                continue;
            }
            config.setUpdateBy(getUsername());
            i += configService.updateConfig(config);
        }
        return toAjax(i);
    }

//    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增参数配置")
    public AjaxResult add(@Validated @RequestBody SysConfig config)
    {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        config.setCompanyId(loginUser.getUser().getDeptId());
        config.setCreateBy(getUsername());
        return toAjax(configService.insertConfig(config));
    }

    @ApiOperation(value = "合同预警配置")
    @PutMapping("/contractWarning")
    public AjaxResult contractWarning(@RequestBody SysConfig config){
        return toAjax(configService.contractWarning(config));
    }


}
